package com.esri.core.symbol;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import com.esri.core.internal.io.handler.n;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.util.d;
import com.esri.core.map.CallbackListener;
import com.tencent.smtt.sdk.TbsVideoCacheTask;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;

/* loaded from: classes.dex */
public class PictureMarkerSymbol extends MarkerSymbol {
    public static final String TYPE = "esriPMS";
    private static final long serialVersionUID = 1;
    String g;
    float h;
    float i;
    transient Drawable j;

    public PictureMarkerSymbol(JsonNode jsonNode) throws Exception {
        super(jsonNode);
        this.g = null;
        this.h = 0.0f;
        this.i = 0.0f;
        this.j = null;
        this.h = this.c / 2.0f;
        this.i = this.d / 2.0f;
        this.g = d.a(jsonNode, TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL);
        byte[] b = d.b(jsonNode, "imageData");
        if (b != null) {
            byte[] decode = Base64.decode(b, 0);
            String str = this.g;
            if (str == null) {
                str = "PMS" + System.nanoTime();
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode);
            this.j = Drawable.createFromStream(byteArrayInputStream, str);
            byteArrayInputStream.close();
        }
    }

    public PictureMarkerSymbol(Context context, Drawable drawable) {
        this.g = null;
        this.h = 0.0f;
        this.i = 0.0f;
        this.j = null;
        this.j = drawable;
        a(context);
    }

    public PictureMarkerSymbol(Drawable drawable) {
        this.g = null;
        this.h = 0.0f;
        this.i = 0.0f;
        this.j = null;
        this.j = drawable;
        a((Context) null);
    }

    public PictureMarkerSymbol(PictureMarkerSymbol pictureMarkerSymbol) {
        super(pictureMarkerSymbol);
        this.g = null;
        this.h = 0.0f;
        this.i = 0.0f;
        this.j = null;
        this.g = pictureMarkerSymbol.getUrl();
        this.j = pictureMarkerSymbol.getDrawable();
        a((Context) null);
    }

    public PictureMarkerSymbol() {
        this.g = null;
        this.h = 0.0f;
        this.i = 0.0f;
        this.j = null;
    }

    @Deprecated
    public PictureMarkerSymbol(String str) throws IOException {
        this.g = null;
        this.h = 0.0f;
        this.i = 0.0f;
        this.j = null;
        this.g = str;
        a(str);
    }

    public void setUrl(String str) {
        this.g = str;
    }

    public String getUrl() {
        return this.g;
    }

    public Drawable fetchDrawable() throws IOException {
        String str = this.g;
        if (str == null) {
            throw new IllegalStateException("URL must be set before calling PictureMarkerSymbol.fetchDrawable()");
        }
        a(str);
        return this.j;
    }

    public Future<Drawable> fetchDrawable(final CallbackListener<Drawable> callbackListener) {
        return e.b.submit(new Callable<Drawable>() { // from class: com.esri.core.symbol.PictureMarkerSymbol.1
            @Override // java.util.concurrent.Callable
            /* renamed from: a */
            public Drawable call() throws Exception {
                try {
                    try {
                        if (Thread.currentThread().isInterrupted()) {
                            throw new InterruptedException();
                        }
                        Drawable fetchDrawable = PictureMarkerSymbol.this.fetchDrawable();
                        CallbackListener callbackListener2 = callbackListener;
                        if (callbackListener2 != null) {
                            callbackListener2.onCallback(fetchDrawable);
                        }
                        return fetchDrawable;
                    } catch (Exception e) {
                        CallbackListener callbackListener3 = callbackListener;
                        if (callbackListener3 != null) {
                            callbackListener3.onError(e);
                        }
                        throw e;
                    }
                } catch (Throwable th) {
                    CallbackListener callbackListener4 = callbackListener;
                    if (callbackListener4 != null) {
                        callbackListener4.onCallback(null);
                    }
                    throw th;
                }
            }
        });
    }

    protected static float getDensityDPI(Context context) {
        if (context != null) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(displayMetrics);
            return displayMetrics.density;
        }
        return 1.0f;
    }

    private void a(Context context) {
        float densityDPI = getDensityDPI(context);
        Drawable drawable = this.j;
        if (drawable != null) {
            if (this.h == 0.0f) {
                this.h = (int) (drawable.getIntrinsicWidth() / (densityDPI * 2.0f));
            }
            if (this.i == 0.0f) {
                this.i = (int) (this.j.getIntrinsicHeight() / (densityDPI * 2.0f));
            }
            float f = this.h;
            if (f <= 0.0f) {
                f = this.i;
            }
            this.h = f;
            float f2 = this.i;
            if (f2 > 0.0f) {
                f = f2;
            }
            this.i = f;
            if (this.c == 0.0f) {
                setWidth(this.h * 2.0f);
            }
            if (this.d == 0.0f) {
                setHeight(this.i * 2.0f);
            }
            if (this.j.getBounds().equals(new Rect(0, 0, 0, 0))) {
                this.j.setBounds(0, 0, (int) this.c, (int) this.d);
            }
        }
    }

    private void a(String str) throws IOException {
        if (str == null) {
            return;
        }
        InputStream inputStream = null;
        try {
            try {
                InputStream b = com.esri.core.internal.io.handler.a.b(str, (Map<String, String>) null, (n) null);
                try {
                    this.j = Drawable.createFromStream(b, str);
                    a((Context) null);
                    if (b != null) {
                        b.close();
                    }
                } catch (Exception e) {
                    e = e;
                    inputStream = b;
                    throw new IOException(e);
                } catch (Throwable th) {
                    th = th;
                    inputStream = b;
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public Drawable getDrawable() {
        return this.j;
    }

    @Override // com.esri.core.symbol.MarkerSymbol
    public int hashCode() {
        int hashCode = ((((super.hashCode() * 31) + Float.floatToIntBits(this.i)) * 31) + Float.floatToIntBits(this.h)) * 31;
        String str = this.g;
        return hashCode + (str == null ? 0 : str.hashCode());
    }

    @Override // com.esri.core.symbol.MarkerSymbol
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (super.equals(obj) && getClass() == obj.getClass()) {
            PictureMarkerSymbol pictureMarkerSymbol = (PictureMarkerSymbol) obj;
            if (Float.floatToIntBits(this.i) == Float.floatToIntBits(pictureMarkerSymbol.i) && Float.floatToIntBits(this.h) == Float.floatToIntBits(pictureMarkerSymbol.h)) {
                String str = this.g;
                if (str == null) {
                    if (pictureMarkerSymbol.g != null) {
                        return false;
                    }
                } else if (!str.equals(pictureMarkerSymbol.g)) {
                    return false;
                }
                return true;
            }
            return false;
        }
        return false;
    }

    @Override // com.esri.core.symbol.Symbol
    public String toJson() throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        d.a(a, "type", TYPE);
        super.a(a);
        String str = this.g;
        if (str != null) {
            d.a(a, TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL, str);
        }
        Drawable drawable = this.j;
        if (drawable != null) {
            if (drawable instanceof BitmapDrawable) {
                a(a, (BitmapDrawable) drawable);
            } else {
                a(a, a());
            }
        }
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    private final BitmapDrawable a() {
        if (this.j != null) {
            Bitmap createBitmap = Bitmap.createBitmap((int) getWidth(), (int) getHeight(), Bitmap.Config.ARGB_8888);
            this.j.draw(new Canvas(createBitmap));
            return new BitmapDrawable(createBitmap);
        }
        return null;
    }

    static void a(JsonGenerator jsonGenerator, BitmapDrawable bitmapDrawable) throws IOException, Exception {
        Bitmap bitmap;
        if (bitmapDrawable == null || (bitmap = bitmapDrawable.getBitmap()) == null) {
            return;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 70, byteArrayOutputStream);
        byteArrayOutputStream.flush();
        d.a(jsonGenerator, "imageData", byteArrayOutputStream.toByteArray());
        d.a(jsonGenerator, "contentType", "image/png");
        byteArrayOutputStream.close();
    }

    @Override // com.esri.core.symbol.Symbol
    public Symbol copy() throws Exception {
        return new PictureMarkerSymbol(this);
    }
}
